package com.kfm.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class RespController {

    @RequestMapping("/mv")
    public ModelAndView mv(){
        ModelAndView mv = new ModelAndView("hello");
        // 存数据
        mv.addObject("msg", "你好");
        return mv;
    }

    @RequestMapping("/mv1")
    public ModelAndView mv(ModelAndView mv){
        // 设置视图
        mv.setViewName("hello");
        // 存数据
        mv.addObject("msg", "你好 mv1");
        return mv;
    }

    @RequestMapping("/mv2")
    public String mv2(Model model){
        model.addAttribute("msg", "model");
        return "hello"; // 逻辑视图
    }


    @RequestMapping("/mv3")
    public String mv3(Model model){
        model.addAttribute("msg", "forward");
        return "forward:/WEB-INF/views/hello.jsp"; // 物理视图 也可以转发到其他 handler
    }

    @RequestMapping("/redirect")
    public String redirect(Model model, RedirectAttributes redirectAttributes){
        model.addAttribute("msg", "redirect");
        // 重定向请求中设置属性
        redirectAttributes.addAttribute("msg", "RedirectAttributes");
        return "redirect:redirect1"; // 重定向到  redirect1
    }

    @RequestMapping("/redirect1")
    public String redirect1(Model model, String msg){
        return "hello";
    }


    @RequestMapping("/redirect2")
    public ModelAndView redirect2(){
        ModelAndView mv = new ModelAndView("redirect:redirect1");
        return mv;
    }


}
